package dk.dtu.student.assignmentfour;

import java.io.IOException;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends Activity {

	private ListView mListView;
	private ListAdapter mListAdapter;
	private ProgressDialog mProgressDialog;
	private Handler handler;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		handler = new Handler();
		
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setCancelable(false);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setIndeterminate(true);
		mProgressDialog.setMessage("Loading SMS messages and evaluating mood scores...");
		
		mListView = (ListView)findViewById(R.id.listView1);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
				String threadId =  mListAdapter.getItem(arg2).getThreadId();
				
				Log.i("MainActivity", "ThreadID Item " + arg3);
				Intent i = new Intent(MainActivity.this, GraphActivity.class);
				Bundle b = new Bundle();
				b.putString("mThreadId", threadId);
				i.putExtras(b);
				startActivity(i);
			}
		});
		
	    new Thread(new Runnable() {
	        public void run() {
	        	handler.post(new Runnable() {
	                public void run() {
	                	mProgressDialog.show();
	                }
	        	});
	        	
	        	MyApplication app = (MyApplication)getApplication();
	    		app.mSmsThreads = app.getAllSms(MainActivity.this);
	    		
	    		try {
	    			Afinn finn = new Afinn(MainActivity.this);
	    			
	    			for (SmsThread t: app.mSmsThreads)
	    			{
	    				String allMessages = "";
	    				for (SmsMessage m: t.getAllSmsMessages())
	    				{
	    					String body = m.getMessageBody();
	    					m.setMood(finn.sentiment(body));
	    					allMessages += " " + body;
	    				}
	    				t.setThreadMood(finn.sentiment(allMessages));
	    			}
	    		} catch (IOException e) {
	    			Log.w("MainActivity", "Afinn failed man!");
	    		}
	    		
	    		mListAdapter = new ListAdapter(MainActivity.this, 0, app.mSmsThreads);
	    		handler.post(new Runnable() {
	                public void run() {
	                	mListView.setAdapter(mListAdapter);
	                	mListView.invalidate();
	                	if (null != mProgressDialog && mProgressDialog.isShowing())
	                		mProgressDialog.dismiss();
	                }
	    		});
	        }
	    }).start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		    case R.id.menu_refresh:
		    	new Thread(new Runnable() {
			        public void run() {
			        	handler.post(new Runnable() {
			                public void run() {
			                	mProgressDialog.show();
			                }
			        	});
			        	
			        	MyApplication app = (MyApplication)getApplication();
				    	app.mSmsThreads = app.getAllSms(MainActivity.this);

				    	handler.post(new Runnable() {
				            @Override 
				            public void run() {
				            	mListView.invalidate();
				            	if (null != mProgressDialog && mProgressDialog.isShowing())
				            		mProgressDialog.dismiss();
				            } 
				        });
			        }
		    	});
		        return true;
	    }
		
		return super.onMenuItemSelected(featureId, item);
	}
}
